<?php

namespace App\Http\Controllers\Index;

use App\Http\Controllers\Collect\CollectController;
use chillerlan\QRCode\QRCode;
use Illuminate\Http\Request;

class IndexController extends CollectController
{

    public function IndexView()
    {
        $installfile = storage_path() . "/app/public/install/install.lock";
        if (!file_exists($installfile)) {
            return redirect('/install');
        }
        $laracvsbanner = $this->Index('indexbanner');
        $laracvsdianying = $this->Kan('dianying', 'all', rand(1, 10));
        $laracvsdianshi = $this->Kan('dianshi', 'all', rand(1, 10));
        $laracvszongyi = $this->Kan('zongyi', 'all', rand(1, 10));
        $laracvsdongman = $this->Kan('dongman', 'all', rand(1, 10));
        $laracvsbd['dianying'] = $this->BdList('dianying', '[class="m-rank g-wrap p-mod js-rank"] .content>ul:eq(1)>li');
        $laracvsbd['dianshi'] = $this->BdList('dianshi');
        $laracvsbd['dongman'] = $this->BdList('dongman');
        $laracvsbd['zongyi'] = $this->BdList('zongyi');
        return view('laracvs.index', ['laracvsbanner' => $laracvsbanner, 'laracvsdianying' => $laracvsdianying, 'laracvsdianshi' => $laracvsdianshi, 'laracvszongyi' => $laracvszongyi, 'laracvsdongman' => $laracvsdongman, 'laracvsbd' => $laracvsbd]);
    }

    public function VideoList($type, $cat, $pg)
    {
        $videolist = $this->Kan($type, $cat, $pg);
        return view('laracvs.type', ['type' => $type, 'cat' => $cat, 'pg' => $pg, 'videolist' => $videolist]);
    }

    public function DetailPlayView($ym, $ztype, $url)
    {
        $data = $this->Play($ztype, $url);
        $type = $data['type'];
        unset($data['type']);
        switch ($type) {
            case "dianying":
                $data[0]['name'] = isset($data[0]['name']) ? $data[0]['name'] : "暂无播放源";
                $data[0]['href'] = isset($data[0]['href']) ? $data[0]['href'] : "http://www.laracvs.com";
                $video = $data[0];
                $like = $data['like'];
                unset($data['like']);
                $player = $data;
                break;
            case "dianshi":
                $video = $data['info'];
                $player = $data['playlist'];
                $like = $data['like'];
                break;
            case "dongman":
                $video = $data['info'];
                $player = $data['playlist'];
                $like = $data['like'];
                break;
            case "zongyi":
                $video = $data[0];
                $like = $data['like'];
                unset($data['like']);
                $player[] = ['name' => $data[0]['name'], 'data' => $data];
                break;
            case "zy":
                $video = $data[0];
                $player = $this->DealPlay($data[0]['playlist']);
                $like = [];
                break;
        }
        $vid = isset($_GET['vid']) ? $_GET['vid'] : "";
        return view('laracvs.' . $ym, ['video' => $video, 'player' => $player, 'type' => $type, 'url' => $url, 'vid' => $vid, 'like' => $like]);
    }

    public function SearchView(Request $request)
    {
        $key = $request->get('cz');
        $videodata = $this->Search($key);
        $laracvsbd['dianying'] = $this->BdList('dianying', '[class="m-rank g-wrap p-mod js-rank"] .content>ul:eq(1)>li');
        $laracvsbd['dianshi'] = $this->BdList('dianshi');
        return view('laracvs.search', ['videolist' => $videodata, 'laracvsbd' => $laracvsbd, 'searchkey' => $key]);
    }

    public function Qrcode(Request $request)
    {
        $url = $request->get('url');
        $qrcode = new QRCode();
        $img = $qrcode->render($url);
        return $img;
    }


}